/*     */ package com.zimbra.cs.account;
/*     */ 
/*     */ import com.google.common.annotations.VisibleForTesting;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.util.Zimbra;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AutoProvisionThread
/*     */   extends Thread
/*     */   implements Provisioning.EagerAutoProvisionScheduler
/*     */ {
/*  30 */   private static volatile AutoProvisionThread autoProvThread = null;
/*  31 */   private static Object THREAD_CONTROL_LOCK = new Object();
/*  32 */   private boolean shutdownRequested = false;
/*     */   
/*     */   @VisibleForTesting
/*     */   protected AutoProvisionThread() {
/*  36 */     setName("AutoProvision");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static synchronized void startup()
/*     */   {
/*  43 */     synchronized (THREAD_CONTROL_LOCK) {
/*  44 */       if (isRunning()) {
/*  45 */         ZimbraLog.autoprov.warn("Cannot start a second auto provision thread while another one is running.");
/*  46 */         return;
/*     */       }
/*     */       
/*  49 */       if (getSleepInterval() == 0L) {
/*  50 */         ZimbraLog.autoprov.info("Not starting auto provision thread because %s is 0.", new Object[] { "zimbraAutoProvPollingInterval" });
/*     */         
/*  52 */         return;
/*     */       }
/*     */       
/*     */       try
/*     */       {
/*  57 */         String displayInterval = Provisioning.getInstance().getLocalServer().getAttr("zimbraAutoProvPollingInterval", null);
/*     */         
/*  59 */         ZimbraLog.autoprov.info("Starting auto provision thread with sleep interval %s.", new Object[] { displayInterval });
/*     */       } catch (ServiceException e) {
/*  61 */         ZimbraLog.autoprov.warn("Unable to get %s.  Aborting thread startup.", "zimbraAutoProvPollingInterval", e);
/*     */         
/*  63 */         return;
/*     */       }
/*     */       
/*     */ 
/*  67 */       autoProvThread = new AutoProvisionThread();
/*  68 */       autoProvThread.start();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static synchronized boolean isRunning()
/*     */   {
/*  76 */     synchronized (THREAD_CONTROL_LOCK) {
/*  77 */       if (autoProvThread != null) {
/*  78 */         return true;
/*     */       }
/*  80 */       return false;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static synchronized void shutdown()
/*     */   {
/*  89 */     synchronized (THREAD_CONTROL_LOCK) {
/*  90 */       if (autoProvThread != null) {
/*  91 */         ZimbraLog.autoprov.info("Shutting down auto provision thread");
/*  92 */         autoProvThread.requestShutdown();
/*  93 */         autoProvThread.interrupt();
/*  94 */         autoProvThread = null;
/*     */       } else {
/*  96 */         ZimbraLog.autoprov.info("shutdown() called, but auto provision thread is not running.");
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static synchronized void switchAutoProvThreadIfNecessary() throws ServiceException {
/* 102 */     Server localServer = Provisioning.getInstance().getLocalServer();
/*     */     
/* 104 */     long interval = localServer.getTimeInterval("zimbraAutoProvPollingInterval", 0L);
/*     */     
/* 106 */     Set<String> scheduledDomains = localServer.getMultiAttrSet("zimbraAutoProvScheduledDomains");
/*     */     
/*     */ 
/* 109 */     boolean needRunning = (interval > 0L) && (!scheduledDomains.isEmpty());
/*     */     
/* 111 */     if ((needRunning) && (!isRunning())) {
/* 112 */       startup();
/* 113 */     } else if ((!needRunning) && (isRunning())) {
/* 114 */       shutdown();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void run()
/*     */   {
/* 128 */     long sleepTime = getInitialDelay();
/* 129 */     ZimbraLog.autoprov.info("Auto provision thread sleeping for %dms before doing work.", new Object[] { Long.valueOf(sleepTime) });
/*     */     try
/*     */     {
/* 132 */       Thread.sleep(sleepTime);
/*     */     } catch (InterruptedException e) {
/* 134 */       ZimbraLog.autoprov.info("Shutting down auto provision thread.");
/* 135 */       autoProvThread = null;
/* 136 */       return;
/*     */     }
/*     */     
/* 139 */     Provisioning prov = Provisioning.getInstance();
/*     */     for (;;)
/*     */     {
/* 142 */       if (isShutDownRequested()) {
/* 143 */         ZimbraLog.autoprov.info("Shutting down auto provision thread.");
/* 144 */         return;
/*     */       }
/*     */       
/* 147 */       long iterStartedAt = System.currentTimeMillis();
/*     */       try
/*     */       {
/* 150 */         prov.autoProvAccountEager(this);
/*     */       } catch (Throwable t) {
/* 152 */         if ((t instanceof OutOfMemoryError)) {
/* 153 */           Zimbra.halt("Ran out of memory while auto provision accounts", t);
/*     */         } else {
/* 155 */           ZimbraLog.autoprov.warn("Unable to auto provision accounts", t);
/*     */         }
/*     */       }
/*     */       
/* 159 */       long iterEndedAt = System.currentTimeMillis();
/* 160 */       long elapsedMillis = iterEndedAt - iterStartedAt;
/*     */       
/*     */ 
/*     */ 
/* 164 */       long sleepInterval = getSleepInterval();
/* 165 */       if (sleepInterval != 0L)
/*     */       {
/* 167 */         if (elapsedMillis < sleepInterval) {
/* 168 */           sleep();
/*     */         } else {
/* 170 */           ZimbraLog.autoprov.debug("Iteration took %d seconds, starting next iteration immediately without sleeping", new Object[] { Long.valueOf(elapsedMillis / 1000L) });
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   @VisibleForTesting
/*     */   protected long getInitialDelay() {
/* 178 */     return LC.autoprov_initial_sleep_ms.longValue();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void sleep()
/*     */   {
/* 186 */     long interval = getSleepInterval();
/* 187 */     ZimbraLog.autoprov.info("Sleeping for %d milliseconds.", new Object[] { Long.valueOf(interval) });
/*     */     
/* 189 */     if (interval > 0L) {
/*     */       try {
/* 191 */         Thread.sleep(interval);
/*     */       } catch (InterruptedException e) {
/* 193 */         ZimbraLog.autoprov.info("Auto provision thread was interrupted.");
/* 194 */         this.shutdownRequested = true;
/*     */       }
/*     */     } else {
/* 197 */       this.shutdownRequested = true;
/*     */     }
/*     */   }
/*     */   
/*     */   private void requestShutdown() {
/* 202 */     this.shutdownRequested = true;
/*     */   }
/*     */   
/*     */   public boolean isShutDownRequested()
/*     */   {
/* 207 */     return this.shutdownRequested;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 214 */   private static long sleepInterval = 0L;
/*     */   
/*     */ 
/*     */ 
/*     */   private static long getSleepInterval()
/*     */   {
/*     */     try
/*     */     {
/* 222 */       Server server = Provisioning.getInstance().getLocalServer();
/* 223 */       sleepInterval = server.getTimeInterval("zimbraAutoProvPollingInterval", 0L);
/*     */     } catch (ServiceException e) {
/* 225 */       ZimbraLog.autoprov.warn("Unable to determine value of %s.  Using previous value: %d.", "zimbraAutoProvPollingInterval", Long.valueOf(sleepInterval), e);
/*     */     }
/*     */     
/*     */ 
/* 229 */     return sleepInterval;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/AutoProvisionThread.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */